﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using System.Collections.Generic;
using System.Web.Services;
using Meqs.Logic;
using Meqs.Utilities;
using Meqs.Logic.Interface;

public partial class Credit_Teacher_Reprots_HistoryClassStudents : ContainerBasePage
{
    protected static int iYear
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["year"], out id))
                return id;
            return -1;
        }
    }
    protected static int iTerm
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["term"], out id))
                return id;
            return -1;
        }
    }
    protected static int iClassid
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["classid"], out id))
                return id;
            return -1;
        }
    }
    protected int index = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindSchoolYear();
            if (iYear == -1)
            {
                selYear_TextChanged(selYear, new EventArgs());
                selClass_TextChanged(selClass, new EventArgs());
            }
            else
            {
                String value = String.Format("{0},{1}", iYear, iTerm);
                selYear.SelectedValue = value;
                selYear_TextChanged(selYear, new EventArgs());
                selClass.SelectedValue = iClassid.ToString();
                btnBind_Click(null,null);
            }
        }
    }

    /// <summary>
    /// 绑定班级下拉框
    /// </summary>
    protected void BindClass(int schoolYear, int term)
    {
        selClass.Items.Clear();
        IList<ClsInfo> schoolList = Container<IClassHistoryLogic>.Instance.FindClassesInSchoolYear(schoolYear, SchoolMark, null);
        ListSort<ClsInfo> sortList = new ListSort<ClsInfo>(schoolList, "Grade");
        schoolList = sortList.Sort();
        if (schoolList == null || schoolList.Count < 1)
            return;

        foreach (ClsInfo info in schoolList)
        {
            String str = "";
            switch (info.Grade)
            {
                case 1:
                    str = "一年级";
                    break;
                case 2:
                    str = "二年级";
                    break;
                case 3:
                    str = "三年级";
                    break;
                case 4:
                    str = "四年级";
                    break;
                case 5:
                    str = "五年级";
                    break;
                case 6:
                    str = "六年级";
                    break;
                case 7:
                    str = "初一";
                    break;
                case 8:
                    str = "初二";
                    break;
                case 9:
                    str = "初三";
                    break;
                case 10:
                    str = "高一";
                    break;
                case 11:
                    str = "高二";
                    break;
                case 12:
                    str = "高三";
                    break;
            }

            #region 班级过滤
            List<int> enableClassList = SchoolCommon.CanViewClasses((int)SecurityTable.Security.Credit_CanView, schoolYear, term);
            bool enableClass = false;
            if (!(enableClassList == null))
            {
                foreach (int i in enableClassList)
                {
                    if (info.RID == i)
                    {
                        enableClass = true;
                        break;
                    }
                }
                if (!enableClass)
                    continue;
            }
            #endregion

            selClass.Items.Add(new ListItem(String.Format("{0}({1})班", str, info.ClsNo), info.RID.ToString()));
        }
        selClass_TextChanged(selClass, new EventArgs());
    }


    /// <summary>
    /// 绑定学期下拉框
    /// </summary>
    protected void BindSchoolYear()
    {
        //Hashtable ht = new Hashtable();
        //ht.Add("SchoolMark", SchoolMark);
        //IList<StuCauseEvalHistory> historyList = Container<IStuCauseEvalHistoryLogic>.Instance.FindAll(ht);
        //if (historyList == null || historyList.Count < 1)
        //{
        //    selYear.Items.Add(new ListItem("没有历史数据", "-1"));
        //}
        //IList<String> strList = new List<String>();
        //if (historyList != null)
        //    foreach (StuCauseEvalHistory history in historyList)
        //    {
        //        String value = String.Format("{0},{1}", history.SchoolYear, history.Term);
        //        if (!strList.Contains(value))
        //        {
        //            int year = 0;
        //            int term = 0;
        //            if (!Int32.TryParse(history.SchoolYear.ToString(), out year))
        //                return;
        //            if (!Int32.TryParse(history.Term.ToString(), out term))
        //                return;
        //            String str = Common.ConvertYearAndTerm(year, term);
        //            selYear.Items.Add(new ListItem(str, value));
        //            strList.Add(value);
        //        }
        //    }

        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);
        IList<TermInfo> terms = Container<ITermInfoLogic>.Instance.FindAll(ht);
        foreach (TermInfo term in terms)
        {
            String str = Common.ConvertYearAndTerm(term.SchoolYear, term.Term);
            String value = String.Format("{0},{1}", term.SchoolYear, term.Term);
            selYear.Items.Add(new ListItem(str, value));
        }
    }

    /// <summary>
    /// 学期下拉框事件
    /// </summary>
    protected void selYear_TextChanged(object sender, EventArgs e)
    {
        int nYear = 0;
        int nTerm = 0;

        string[] outString = selYear.SelectedValue.Split(new char[] { ',' });
        if (outString.Length != 2)
            return;

        if (!Int32.TryParse(outString[0], out nYear))
            return;

        if (!Int32.TryParse(outString[1], out nTerm))
            return;

        BindClass(nYear, nTerm);
    }

    /// <summary>
    /// 学期下拉框事件
    /// </summary>
    protected void selClass_TextChanged(object sender, EventArgs e)
    {
        int nYear = 0;
        int nTerm = 0;
        int nClass = 0;

        string[] outString = selYear.SelectedValue.Split(new char[] { ',' });
        if (outString.Length != 2)
            return;

        if (!Int32.TryParse(outString[0], out nYear))
            return;

        if (!Int32.TryParse(outString[1], out nTerm))
            return;

        if (!Int32.TryParse(selClass.SelectedValue, out nClass))
            return;

        Hashtable ht = new Hashtable();
        ht.Add("RID", nClass);
        ht.Add("SchoolMark", SchoolMark);
        IList<ClsInfo> cilist = Container<IClsInfoLogic>.Instance.FindAll(ht);
    }
    int ischoolYear, iterm, iclsId = 0;
    protected void btnBind_Click(object sender, EventArgs e)
    {
        
        string[] outString = selYear.SelectedValue.Split(new char[] { ',' });
        if (outString.Length != 2)
            return;

        if (!Int32.TryParse(outString[0], out ischoolYear))
            return;

        if (!Int32.TryParse(outString[1], out iterm))
            return;

        if (!Int32.TryParse(selClass.SelectedValue, out iclsId))
            return;


        //int id = Convert.ToInt32(clsId);
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        Hashtable ht = new Hashtable();
        ht.Add("ClassID", iclsId);
        ht.Add("SchoolYear", Convert.ToInt32(ischoolYear));
        ht.Add("Term", iterm);


        sinfo = Container<IStudentHistoryLogic>.Instance.FindHistoryStudents(
            Convert.ToInt32(ischoolYear),
            Convert.ToInt32(iterm),
            iclsId, SchoolMark);
        ids = new List<int>();
        Repeater1.DataSource = sinfo;
        Repeater1.DataBind();

    }
    IList<StudentInfo> sinfo = new List<StudentInfo>();
    private List<int> ids = new List<int>();
    protected string GetHeader()
    {
        string str = "<tr>";
        //str += "<th>姓名</th><th>学号</th><th>学业档案</th><th>德育档案</th>";
        str += "<th>姓名</th><th>学号</th><th>姓名</th><th>学号</th>";
        str += "</tr>";
        return str;
    }
    protected string GetTeacher(int id, string name, string coder)
    {
        if (ids.Contains(id))
        {
            return "";
        }
        string str = "<tr onclick='SetHightLight(this);'>";
        str += "<td><a src='#' onclick=\"ShowStudent(" + id + ",'" + coder + "','" + HttpUtility.UrlEncode(name) + "'," + ischoolYear + "," + iterm + "," + iclsId + ")\">" + name + "</a></td>";
        str += "<td>" + coder + "</td>";
        ids.Add(id);
        StudentInfo student = getnextStudent(id);
        if (student != null)
        {
            ids.Add(student.RID);
            str += "<td><a src='#' onclick=\"ShowStudent(" + student.RID + ",'" + student.StudentCode + "','" + HttpUtility.UrlEncode(student.StudentName) + "'," + ischoolYear + "," + iterm + "," + iclsId + ")\">" + student.StudentName + "</a></td>";
            str += "<td>" + student.StudentCode + "</td>";
        }
        else
        {
            str += "<td></td>";
            str += "<td></td>";
        }

        //str += "<td ><a src='#' onclick=\"ShowStudent(" + id + ",'" + coder + "','" + HttpUtility.UrlEncode(name) + "')\">查看</a></td>";
        //str += "<td ><a src='#' onclick=\"ShowStudentFile(" + id + ",'" + coder + "','" + HttpUtility.UrlEncode(name) + "')\">查看</a></td></tr>";
        str += "</tr>";
        return str;
    }

    private StudentInfo getnextStudent(int id)
    {
        for (int i = 0; i < sinfo.Count; i++)
        {
            if (sinfo[i].RID == id)
            {
                if (i != sinfo.Count - 1)
                {
                    return sinfo[i + 1];
                }
            }
        }
        return null;
    }
}
